import {
  getUserProfessionCourse,
  getRecruit,
  getSubNav,
  getBanner,
  getProfessionHome,
  getScBanner,
  getCertificater,
  getSpecialTopicList,
  getProfessionNews,
  getProfessionTeachers,
  getShicaoRecommend,
  getResearchCenter
} from '@/api/kjsc'
let professionId = '21'
let new_profession_id = '5'
let config = {
  kjsc_cont: {
    module_name: '会计实操',
    path_name: 'kjsc',
    heads: {
      title: '会计实操_初级会计考试学习_之了课堂',
      keywords:
        '会计实操,会计记账课程,全盘账务处理与纳税申报,小规模全盘账务,零基础做账',
      description:
        '之了课堂-会计实操提供全盘账务处理与纳税申报、小规模全盘账务等课程，零基础手把手教做账。'
    },
    profession_id: 21,
    kjsc_menus: [
      {},
      {
        title: '财税政策',
        en: 'policy',
        nav_id: 11
      },
      {
        title: '热点干货',
        en: 'infos',
        nav_id: 10
      },
      {
        title: '会计实务',
        en: 'kjsw',
        nav_id: 18
      },
      {
        title: '就业指导',
        en: 'employment',
        nav_id: 19
      },
      {
        title: '实操问答',
        en: 'answer',
        nav_id: 20
      }
    ],
    banners: [
      {
        pic_url:
          '//image.zlketang.com/public/news/images/guanwangPic/kjsc_banner.jpg',
        path:
          'https://wap.zlketang.com/public/wxpub/page/zl_course/commodity.html?fromShop=1&product_id=2020003000349&course_id=349&source_page_subject=%E5%95%86%E5%9F%8E%E9%A6%96%E9%A1%B5&source_page_id='
      }
    ],
    hots: [
      {
        title: '2019初级会计新课开奖啦！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '2019初级会计新课开奖啦！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '2019初级会计新课开奖啦！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '2019初级会计新课开奖啦！',
        news_time: '11/21',
        path: ''
      }
    ],
    stuffs: [
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      },
      {
        title: '重磅！2019年初级会计职称考试大纲已发布！',
        news_time: '11/21',
        path: ''
      }
    ],
    pic_entry: [
      {
        pic_url:
          '//image.zlketang.com/public/news/images/guanwangPic/cjkj-quick.jpg',
        path: '/cjkjPlan'
      },
      {
        pic_url:
          '//image.zlketang.com/public/news/images/guanwangPic/course-entry.png',
        path: '/courses/?profession_id=' + professionId
      },
      {
        pic_url: '/news/images/global/tiku-entry.png',
        path: '/exercises/?profession_id=' + professionId
      }
    ],
    courses: [
      {
        course_name: '一般纳税人全盘账务处理+纳税申报',
        course_pic:
          '//image.zlketang.com/public/news/images/guanwangPic/kjsc-1.jpg',
        origin_price: '1188',
        now_price: '558',
        path: '/vplay/?course_id=148'
      },
      {
        course_name: '会计实操小规模纳税人全盘账务处理+税务申报',
        course_pic:
          '//image.zlketang.com/public/news/images/guanwangPic/kjsc-2.jpg',
        origin_price: '599',
        now_price: '188',
        path: '/vplay/?course_id=147'
      }
    ]
  }
}
let kjsc_cont = config.kjsc_cont
let kjsc_menus = kjsc_cont.kjsc_menus
let path = kjsc_cont.path_name
const subjects = {
  1: {
    title: '初级会计职称',
    name: '初级会计',
    courseName: '初级会计',
    en: 'cjkj',
    short: '初级',
    color: '#03a9f4',
    icon: '初',
    iconfont: '#iconchujihuiji',
    profession_id: '4',
    planName: '初级会计'
  },
  2: {
    title: '中级会计职称',
    name: '中级会计',
    courseName: '中级会计',
    en: 'zjkj',
    short: '中级',
    color: '#ff9800',
    icon: '中',
    iconfont: '#iconzhong',
    profession_id: '6',
    planName: '中级会计'
  },
  3: {
    title: '注册会计师',
    name: '注册会计',
    courseName: 'CPA课程',
    en: 'cpa',
    short: '注会',
    color: '#9c27b0',
    icon: '注',
    iconfont: '#iconzhu',
    profession_id: '7',
    planName: '注册会计师'
  },
  4: {
    title: '税务师',
    name: '税务师',
    courseName: '税务师',
    en: 'cta',
    short: '税务师',
    color: '#8bc34a',
    icon: '税',
    iconfont: '#iconshui',
    profession_id: '22',
    planName: '税务师'
  },
  5: {
    title: '会计实操',
    name: '会计实操',
    courseName: '实操课程',
    en: 'kjsc',
    color: '#5677fc',
    icon: '实',
    short: '实操',
    iconfont: '#iconshi',
    profession_id: '21'
  },
  6: {
    //新加专业
    title: '管理会计初级',
    name: '管理会计',
    courseName: '管理会计',
    en: 'mat_g',
    color: '#5677fc',
    icon: '管',
    short: '管会',
    iconfont: '#iconguan',
    profession_id: '23',
    planName: '管理会计师'
  },
  7: {
    title: '中级经济师',
    name: '中级经济师',
    courseName: '中级经济师',
    en: 'zjjjs',
    color: '#5677fc',
    icon: '中',
    short: '中级经济',
    //TOOD 换图标
    iconfont: '#iconjing',
    profession_id: '24',
    planName: '中级经济师'
  },
  8: {
    title: 'CMA',
    name: 'CMA',
    courseName: 'CMA',
    en: 'cma',
    color: '#5677fc',
    icon: 'C',
    short: 'CMA',
    //TOOD 换图标
    iconfont: '#iconcma',
    profession_id: '57',
    planName: 'CMA'
  }
}
const submenuMap = {
  1: { title: '考试动态', en: 'dynamic' },
  2: { title: '考试指南', en: 'guide' },
  3: { title: '考试政策', en: 'policy' },
  4: { title: '行业动态', en: 'infos' },
  5: { title: '考试教材', en: 'book' },
  6: { title: '热门活动', en: 'course' },
  7: { title: '名师指导', en: 'learn' },
  8: { title: '备考经验', en: 'experience' },
  9: { title: '考试干货', en: 'stuff' }
}

const training = {
  12: { title: '历年真题', en: 'exam' },
  13: { title: '模拟试卷', en: 'simulation' },
  14: { title: '章节题库', en: 'chapter' },
  15: { title: '重点归纳', en: 'keynote' },
  16: { title: '每月一练', en: 'month' },
  17: { title: '考试练习', en: 'practice' }
}
const kjscMap = {
  10: { title: '热点干货', en: 'infos' },
  11: { title: '财税政策', en: 'policy' },
  18: { title: '会计实务', en: 'kjsw' },
  19: { title: '就业指导', en: 'employment' },
  20: { title: '实操问答', en: 'answer' }
}

let submenu_map = {
  1: { title: '考试动态', en: 'dynamic' },
  2: { title: '考试指南', en: 'guide' },
  3: { title: '考试政策', en: 'policy' },
  4: { title: '行业动态', en: 'infos' },
  5: { title: '考试教材', en: 'book' },
  6: { title: '热门活动', en: 'course' },
  7: { title: '名师指导', en: 'learn' },
  8: { title: '备考经验', en: 'experience' },
  9: { title: '考试干货', en: 'stuff' },
  12: { title: '历年真题', en: 'exam' },
  13: { title: '模拟试卷', en: 'simulation' },
  14: { title: '章节题库', en: 'chapter' },
  15: { title: '重点归纳', en: 'keynote' },
  16: { title: '每月一练', en: 'month' },
  17: { title: '考试练习', en: 'practice' },
  10: { title: '热点干货', en: 'infos' },
  11: { title: '财税政策', en: 'policy' },
  18: { title: '会计实务', en: 'kjsw' },
  19: { title: '就业指导', en: 'employment' },
  20: { title: '实操问答', en: 'answer' }
}

let toolFn = {
  sliceArray(array, n) {
    if (Array.isArray(array) && array.length == 0) {
      return []
    }
    var num = n || 5
    var result = []
    var round = array.length / num
    if (round <= 1) {
      result.push(array)
      var res = result
      return res
    }
    for (var i = 0; i < round; i++) {
      var start = i * num
      var end = start + num
      result.push(array.slice(start, end))
    }
    return result
  },
  connecturl(profession_id, nav_id, id) {
    //拼接地址
    let path_string = ''
    for (var el1 in subjects) {
      if (+profession_id === +el1 && subjects[el1]) {
        path_string += '/' + subjects[el1].en
      }
    }

    for (var el2 in submenu_map) {
      if (+nav_id === +el2 && submenu_map[el2]) {
        path_string += '/' + submenu_map[el2].en
      }
    }

    return path_string + '_detail/' + id + '.html'
  }
}
export default {
  namespaced: true,
  state: {
    kjscPlanInfo: {}
  },
  mutations: {
    setInfo(state, data) {
      state.kjscPlanInfo = data
    }
  },
  actions: {
    async getInfo({ commit }, params) {
      let { path, query } = params.route
      let context = params.context
      var professionCourse = {}

      try {
        professionCourse = await getUserProfessionCourse({
          profession_id: professionId
        })
      } catch (e) {}

      let coursesRecommend =
          (professionCourse.data && professionCourse.data.recommend) || [],
        // 页面展示课程数组
        course = [] // 对实操课程处理，只展示两门不免费的课程

      coursesRecommend.forEach(coursesItem => {
        if (+coursesItem.is_free === 0 && course.length < 2) {
          course.push(coursesItem)
        }
      })
      var recruitData = {}
      try {
        recruitData = await getRecruit()
      } catch (e) {}
      let header_nav = recruitData.data.sub_nav
      var sub_nav

      try {
        sub_nav = await getSubNav()
      } catch (e) {} //获取会计实操栏目页广告

      var ad = {}
      try {
        ad = await getBanner()
      } catch (e) {}

      let advertisement = [],
        leftBanner = [],
        topBanner = [],
        middleBanner = []

      if (ad && ad.data) {
        let totalData = ad.data.ad

        if (totalData) {
          leftBanner = totalData.sc_lb
          topBanner = totalData.sc_top_banner
          middleBanner = totalData.sc_middle_banner
          advertisement = ad.data.ad.zysy
        }
      } // 热点干货

      let infos = {
          list: [],
          name: '热点干货',
          en: 'infos',
          nav_id: 10
        },
        // 财税政策
        policy = {
          list: [],
          name: '财税政策',
          en: 'policy',
          nav_id: 11
        },
        kjsw = {
          list: [],
          name: '会计实务',
          en: 'kjsw',
          nav_id: 18
        },
        employment = {
          list: [],
          name: '就业指导',
          en: 'employment',
          nav_id: 19
        },
        answer = {
          list: [],
          name: '实操问答',
          en: 'answer',
          nav_id: 20
        },
        important = [],
        //重要资讯
        comment = [],
        //获取学员评论
        teacher_detail = [] //老师试听列表
      //获取相关专业下重要资讯，免费试听，评论等

      var profession_data = {}

      try {
        profession_data = await getProfessionHome({
          profession_id: new_profession_id
        })
      } catch (e) {}

      if (profession_data.data) {
        let professionData = JSON.parse(JSON.stringify(profession_data.data)) // 获取重要资讯

        important = professionData.important || []
        important.forEach(function(item) {
          kjsc_menus.forEach(function(item1) {
            if (+item.nav_id === +item1.nav_id) {
              item.name = item1.title
            }
          })
        }) // 获取所有同学的评论

        comment = professionData.comment || [] // 获取专业下的免费试听

        teacher_detail = professionData.teacherListen || []
      }
      //获取主页顶部轮播
      var homeBanner = {}

      try {
        homeBanner = await getScBanner()
      } catch (e) {}

      let bannerData = homeBanner.data || []
      //获取证书现状
      var certificates

      try {
        certificates = await getCertificater({
          profession_id: 5
        })
      } catch (e) {}
      if (certificates) {
        for (var key in certificates.data) {
          let s = certificates.data[key].desc.split('，')
          certificates.data[key].descs = s
        }
      }
      let certificate = certificates.data || []
      // 获取当前的专业所有专题
      var specialTopicListResult
      try {
        // 获取栏目页广告
        specialTopicListResult = await getSpecialTopicList({
          profession_id: new_profession_id
        })
      } catch (e) {}

      let specialTopicList = specialTopicListResult.data || []
      // 链接拼接

      try {
        specialTopicList.forEach(topic => {
          topic.news.forEach(newsItem => {
            newsItem.jump_path = toolFn.connecturl(
              newsItem.profession_id,
              newsItem.nav_id,
              newsItem.object_id
            )
          })
        })
      } catch (error) {} // 获取当前专业的咨询

      var professionData

      try {
        professionData = await getProfessionNews({
          area_id: 1,
          profession_id: 5
        })
      } catch (e) {}

      if (professionData && professionData.data) {
        professionData.data.forEach(professionDataItem => {
          switch (professionDataItem.nav_id) {
            // 热点干货
            case 10:
              infos.list = professionDataItem.list || []
              break
            // 财税政策

            case 11:
              policy.list = professionDataItem.list || []
              break
            // 会计实务

            case 18:
              kjsw.list = professionDataItem.list || []
              break
            // 就业指导

            case 19:
              employment.list = professionDataItem.list || []
              break
            // 就业指导

            case 20:
              answer.list = professionDataItem.list || []
              break
          }
        })
      }
      // 获取实操的老师和学员评论
      var teacher_res

      try {
        teacher_res = await getProfessionTeachers({
          profession_id: 5
        })
      } catch (e) {}

      let teacher = []

      if (teacher_res && teacher_res.data) {
        teacher = teacher_res.data
      }
      //获取实操学院的推荐商品
      let shicaoRecommendList = []
      var shicaoRecommendRes

      try {
        shicaoRecommendRes = await getShicaoRecommend()
      } catch (e) {}

      if (shicaoRecommendRes && shicaoRecommendRes.data) {
        shicaoRecommendList = shicaoRecommendRes.data
      }
      //获取考证中心数据
      let researchCenterList = []

      var researchCenterRes

      try {
        researchCenterRes = await getResearchCenter()
      } catch (e) {}

      if (researchCenterRes && researchCenterRes.data) {
        researchCenterList = researchCenterRes.data || []
        //过滤会计实操专业
        researchCenterList = researchCenterList.filter(function(item) {
          return +item.profession_id !== 5
        })
        // 链接拼接
        researchCenterList.forEach(function(item, index) {
          if (subjects[item.profession_id]) {
            item.path = subjects[item.profession_id].en
            item.name = subjects[item.profession_id].name
          }

          item.news_list.forEach(function(nitem, nindex) {
            nitem.jump_path = toolFn.connecturl(
              nitem.profession_id,
              nitem.nav_id,
              nitem.id
            )
            Object.keys(submenu_map).forEach(function(item1, index2) {
              if (+item1 === +nitem.nav_id) {
                nitem.name = submenu_map[item1].title
              }
            })
          })
        })
      }
      let data = {
        /* heads: config.pages[path].heads,
        module_name: config.pages[path].module_name,
        path_name: config.pages[path].path_name, */
        banners: leftBanner,
        //顶部左边轮播
        topBanner,
        //顶部中间广告
        middleBanner,
        //中部广告
        advertisement,
        // 会计实操的九宫格的题库和领取资料都走初级会计
        path: 'kjsc',
        professionPath: '',
        profession_id: professionId,
        // createStaticUrl: tool.createStaticUrl,
        // connectUrl: tool.connectUrl,
        header_nav,
        sub_nav: sub_nav.data,
        new_profession_id: 5,
        // 热点干货
        infos,
        // 财税政策
        policy,
        //会计实务
        kjsw,
        //就业指导
        employment,
        //实操问答
        answer,
        // 实操页面九宫格的领取资料默认到初级会计实务
        materials_path: 'sw',
        // 实操课程
        course,
        // 实操老师
        teacher,
        // 将老师4个一组
        teacher_list: toolFn.sliceArray(teacher, 4),
        message: 'message',
        //设定跳转链为更多资讯关键字
        important,
        comment,
        bannerData,
        //证书现状
        certificate,
        teacher_detail,
        specialTopicList,
        shicaoRecommendList,
        //实操学院商品列表
        researchCenterList //考证中心列表
      }
      commit('setInfo', data)
      return true
    }
  },
  getters: {}
}
